/**
 * 管理初始化
 */
var PolicyPlan = {
    id: "PolicyPlanTable",	//表格id
    policyId: $("#policyId").val(),	        //保单ID
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PolicyPlan.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'Id', visible: false, align: 'center', valign: 'middle'},
        {title: '保障计划代码', field: 'policy_plan_code', visible: true, align: 'center', valign: 'middle'},
        {title: '保障计划名称', field: 'policy_plan_name', visible: true, align: 'center', valign: 'middle'},
        {title: '险种', field: 'insurance_names', visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
PolicyPlan.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        PolicyPlan.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
PolicyPlan.openAddPolicyPlan = function () {
    var index = layer.open({
        type: 2,
        title: '添加保障计划',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/policyPlan/policyPlan_add/' + PolicyPlan.policyId
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
PolicyPlan.editPolicyPlanDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改保障计划',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policyPlan/policyPlan_update/' + PolicyPlan.seItem.id +'/1'
        });
        this.layerIndex = index;
    }
};
/**
 * 打开查看详情
 */
PolicyPlan.openPolicyPlanDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '保障计划详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policyPlan/policyPlan_update/' + PolicyPlan.seItem.id +'/2'
        });
        this.layerIndex = index;
    }
};

/**
 * 选择险种
 */
PolicyPlan.openAddInsuranceKind = function (status) {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '选择险种',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policyPlan/add_insurance_kind/' + PolicyPlan.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
PolicyPlan.delete = function () {
    if (this.check()) {
        Feng.confirm("是否删除保障计划 "+this.seItem.policy_plan_name+"（"+ this.seItem.policy_plan_code +"）?",function () {
            var ajax = new $ax(Feng.ctxPath + "/policyPlan/delete", function (data) {
                Feng.success("删除成功!");
                PolicyPlan.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("policyPlanId",PolicyPlan.seItem.id);
            ajax.start();
        })
    }
};

/**
 * 查询列表
 */
PolicyPlan.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    PolicyPlan.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = PolicyPlan.initColumn();
    var table = new BSTable(PolicyPlan.id, "/policyPlan/list/"+PolicyPlan.policyId, defaultColunms);
    table.setPaginationType("client");
    PolicyPlan.table = table.init();
});
